package com.molyfun.parents.activity.report.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.iepPlan.IProductDetailTabCell;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/12/21 0021.
 */

public class PlanDetailCallListAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    Context mContext;
    private List<T> mList;

    private int mCurrentTab = -1;

    private PlanDetailCallListCallback mPlanDetailTabCallback;

    public PlanDetailCallListAdapter(Context context, PlanDetailCallListCallback planDetailTabCallback) {
        this.mContext = context;
        this.mPlanDetailTabCallback = planDetailTabCallback;
    }

    public void refreshData(List<T> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    public void clearAll(){
        if(mList != null){
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<T> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PlanDetailCallListBriefViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_plan_detail_cell_list, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final IProductDetailTabCell planDetailBean = (IProductDetailTabCell)mList.get(position);
        final PlanDetailCallListBriefViewHolder planBriefViewHolder = (PlanDetailCallListBriefViewHolder)holder;
        planBriefViewHolder.mProductDetailTabName.setText(planDetailBean.getName());

        if(mCurrentTab == position){
            planBriefViewHolder.mDrawerBtn.setImageResource(R.mipmap.ic_table_up_srrow);
        }else{
            planBriefViewHolder.mDrawerBtn.setImageResource(R.mipmap.ic_table_down_srrow);
        }

        planBriefViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mPlanDetailTabCallback != null){

                    if(mCurrentTab == position){
                        planBriefViewHolder.mDetailRecyclerView.setVisibility(View.GONE);
                        mCurrentTab = -1;
                    }else{
                        planBriefViewHolder.mDetailRecyclerView.setVisibility(View.VISIBLE);
                        mPlanDetailTabCallback.onPlanDetailCallListClick(position,planBriefViewHolder.mPlanDetailCallListCotentAdapter);
                        mCurrentTab = position;
                    }
                    notifyDataSetChanged();
                }
            }
        });
    }

    public interface PlanDetailCallListCallback{
        void onPlanDetailCallListClick(int position,PlanDetailCallListCotentAdapter planDetailCallListCotentAdapter);
    }

    public class PlanDetailCallListBriefViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.product_detail_tab_name)
        TextView mProductDetailTabName;
        @InjectView(R.id.detail_recyclerView)
        RecyclerView mDetailRecyclerView;
        @InjectView(R.id.drawer_btn)
        ImageView mDrawerBtn;

        private PlanDetailCallListCotentAdapter mPlanDetailCallListCotentAdapter;

        public PlanDetailCallListBriefViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
            LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(mContext);
            mLinearLayoutManager.setAutoMeasureEnabled(true);
            mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mDetailRecyclerView.setLayoutManager(mLinearLayoutManager);
            mDetailRecyclerView.setHasFixedSize(true);

            mPlanDetailCallListCotentAdapter = new PlanDetailCallListCotentAdapter(mContext);
            mDetailRecyclerView.setAdapter(mPlanDetailCallListCotentAdapter);

        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

}
